<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="text-center avatar-box">
        <img src="../assets/logo.png" class="img-thumbnail avatar" alt="" />
      </div>

      <!-- 表单区域 -->
      <div class="form-login p-4">
        <!-- 登录名称 -->
        <div class="form-group form-inline">
          <label for="username">登录名称</label>
          <input
            v-model.trim="username"
            type="text"
            class="form-control ml-2"
            id="username"
            placeholder="请输入登录名称"
            autocomplete="off"
          />
        </div>
        <!-- 登录密码 -->
        <div class="form-group form-inline">
          <label for="password">登录密码</label>
          <input
            v-model.trim="password"
            type="password"
            class="form-control ml-2"
            id="password"
            placeholder="请输入登录密码"
          />
        </div>
        <!-- 登录和重置按钮 -->
        <div class="form-group form-inline d-flex justify-content-end">
          <button type="button" class="btn btn-secondary mr-2">重置</button>
          <button @click="login" type="button" class="btn btn-primary">
            登录
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 导入axios
import axios from "axios";

export default {
  name: "MyLogin",
  data() {
    return {
      username: "",
      password: "",
    };
  },

  methods: {
    login() {
      // 判断一下有没有输入完？应该先非空判断
      if (this.username == "" || this.password == "")
        return alert("请输入完整的内容");

      // 能来到这，代表输入了内容
      // 就可以发请求去做登录了
      axios({
        url: "https://toutiao.itheima.net/v1_0/authorizations",
        method: "post",
        // post请求给参数，在axios里怎么给？
        data: {
          mobile: this.username,
          code: this.password,
        },
      })
        .then((res) => {
          // 并且token保存起来
          window.localStorage.setItem('token', res.data.data.token)
          // 跳转到首页
          this.$router.push('/')
        })
        .catch(() => {
         
          alert('账号或密码错误')
        });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background-color: #35495e;
  height: 100%;
  .login-box {
    width: 400px;
    height: 250px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
    .form-login {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      box-sizing: border-box;
    }
  }
}

.form-control {
  flex: 1;
}

.avatar-box {
  position: absolute;
  width: 100%;
  top: -65px;
  left: 0;
  .avatar {
    width: 120px;
    height: 120px;
    border-radius: 50% !important;
    box-shadow: 0 0 6px #efefef;
  }
}
</style>